package com.os.start.controller;

import com.os.core.common.JsonResult;
import com.os.start.pojo.Resource;
import com.os.start.pojo.User;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;
import java.util.HashMap;

/**
 * 描述:
 * RestController针对当前控制器所有的返回值都会以一个字符串返回相当于方法上方添加@ResponseBody注解
 * springBoot默认使用的是jackson
 *
 * @author Matt
 * @create 2021-05-09 18:38
 */
@RestController
public class HelloController {

    //将配置文件属性值注入到代码中
    //@Value("${com.os.hello.springBoot}")
    private String url;

    @Autowired
    private Resource resource;

    @RequestMapping("/getResource")
    public JsonResult getResource(){
        Resource resourceBean = new Resource();
        BeanUtils.copyProperties(resource, resourceBean);
        return JsonResult.returnResponse(resourceBean);
    }

    @RequestMapping("/getUrl")
    public String getUrl(){
        return url;
    }

    @RequestMapping("/hello")
    public String hello(){
        return "hello spring boot!";
    }

    @RequestMapping("/helloMap")
    //@ResponseBody
    public HashMap<String, Object> helloMap(){
        HashMap<String, Object> map = new HashMap<>();
        map.put("hello", "hello world");
        return map;
    }

    @RequestMapping("/getUserJson")
    public User getUserJson(){
        User user = new User();
        user.setName("Matt");
        user.setAge(18);

        return user;
    }

    @RequestMapping("/getUserJsonResult")
    public JsonResult getUserJsonResult(){
        User user = new User();
        user.setName("Matt");
        user.setAge(18);
        user.setPassword("123456");
        user.setDesc("i am a desc ");
        user.setBirthday(new Date());

        return new JsonResult(true, user);
    }

    @RequestMapping("/hotRelease")
    public JsonResult hotRelease(){
        User user = new User();
        user.setName("Matt ~");
        user.setAge(18);
        user.setPassword("123456");
        user.setDesc("i am a desc");
        user.setBirthday(new Date());

        return new JsonResult(true, user);
    }

    @RequestMapping("/getProperties")
    public JsonResult getProperties(){
        return new JsonResult(true, url);
    }

    @RequestMapping("/getResources")
    public JsonResult getResources(){
        Resource resourceBean = new Resource();
        BeanUtils.copyProperties(resource, resourceBean);
        return new JsonResult(true, resourceBean);
    }

}
